
const { baseUrl } = require('./env.js').dev;
let isRefreshing = true;
export class Http {
  constructor() { }

  request({
    url,
    data = {},
    method,
    header,
    callback = '',
    type,
  } = {}) {
    let _url = `${baseUrl}/${url}`;
    return new Promise((resolve, reject) => {
      wx.request({
        url: _url,
        data,
        method,
        header: {
          'content-type': type,
        },
        callback,
        fail(res) {
          reject('发送失败');
          wx.showToast({
            title: '网络错误',
            icon: 'none',
            duration: 2000
          })
        },
        complete: res => {
          setTimeout(function () {
            // wx.hideLoading()
          }, 300)

          if (callback) return callback(res.data);

          if (res.data.code == 200) {
            resolve(res.data);
          }
          isRefreshing = false;
          if (res.data.code == 500) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          } else
            if (res.data.code == 404) {
              wx.showToast({
                title: '参数效验失败',
                icon: 'none'
              })
            } else
              if (res.data.code == 403) {
                wx.showToast({
                  title: '没有相关权限',
                  icon: 'none'
                })
              } else
                if (res.data.code == 402) {
                  wx.hideLoading();

                } else {

                }
        }
      })
    })
  }
}
